package com.tom.tran;




import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;


public class Producer {
    public static void main(String[] args) throws Exception {
        // 创建事务建造者对象
        TransactionMQProducer producer = new TransactionMQProducer("tran_hello_group");
        // 获取mq连接
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 启动生产者
        producer.start();
        producer.setTransactionListener(new MyListener());
        // 4.1.创建消息体
        // topic：消息主题 tags：此主题下的二级分类 body：消息的字节
        Message message = new Message("tran", "hello", "源码时代".getBytes(StandardCharsets.UTF_8));
        // 发送消息
        SendResult sendResult  = producer.sendMessageInTransaction(message, null);
        System.out.println(sendResult);
        producer.shutdown();

    }
}
